﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace BoneAnimationProt
{
    public class InputHandler : Microsoft.Xna.Framework.GameComponent
    {
        #region Keyboard Field Region
        static KeyboardState keyboardState;
        static KeyboardState lastKeyboardState;
        #endregion
        #region Game Pad Field Region
        static GamePadState[] gamePadStates;
        static GamePadState[] lastGamePadStates;
        #endregion
        #region Mouse Field Region
        static MouseState mouseState;
        static MouseState lastMouseState;
        #endregion
        #region Keyboard Property Region
        public static KeyboardState KeyboardState
        {
            get { return keyboardState; }
        }
        public static KeyboardState LastKeyboardState
        {
            get { return lastKeyboardState; }
        }
        #endregion
        #region Game Pad Property Region
        public static GamePadState[] GamePadStates
        {
            get { return gamePadStates; }
        }
        public static GamePadState[] LastGamePadStates
        {
            get { return lastGamePadStates; }
        }
        #endregion
        #region Mouse Property Region
        public static MouseState MouseState
        {
            get { return mouseState; }
        }
        public static MouseState LastMouseState
        {
            get { return lastMouseState; }
        }
        public static Vector2 MousePosition
        {
            get { return new Vector2(mouseState.X, mouseState.Y); }
        }
        #endregion
        #region Constructor Region
        public InputHandler(Game game)
            : base(game)
        {
            keyboardState = Keyboard.GetState();
            gamePadStates = new GamePadState[Enum.GetValues(typeof(PlayerIndex)).Length];
            foreach (PlayerIndex index in Enum.GetValues(typeof(PlayerIndex)))
                gamePadStates[(int)index] = GamePad.GetState(index);

            mouseState = Mouse.GetState();
        }
        #endregion
        #region XNA methods
        public override void Initialize()
        {
            base.Initialize();
        }
        public override void Update(GameTime gameTime)
        {
            lastKeyboardState = keyboardState;
            keyboardState = Keyboard.GetState();
            lastGamePadStates = (GamePadState[])gamePadStates.Clone();
            foreach (PlayerIndex index in Enum.GetValues(typeof(PlayerIndex)))
                gamePadStates[(int)index] = GamePad.GetState(index);
            lastMouseState = mouseState;
            mouseState = Mouse.GetState();
            base.Update(gameTime);
        }
        #endregion
        #region General Method Region
        public static void Flush()
        {
            lastKeyboardState = keyboardState;
            lastMouseState = mouseState;
        }
        #endregion
        #region Keyboard Region
        public static bool KeyReleased(Keys key)
        {
            return keyboardState.IsKeyUp(key) &&
            lastKeyboardState.IsKeyDown(key);
        }
        public static bool KeyPressed(Keys key)
        {
            return keyboardState.IsKeyDown(key) &&
            lastKeyboardState.IsKeyUp(key);
        }
        public static bool KeyDown(Keys key)
        {
            return keyboardState.IsKeyDown(key);
        }
        #endregion
        #region Game Pad Region
        public static bool ButtonReleased(Buttons button, PlayerIndex index)
        {
            return gamePadStates[(int)index].IsButtonUp(button) &&
            lastGamePadStates[(int)index].IsButtonDown(button);
        }
        public static bool ButtonPressed(Buttons button, PlayerIndex index)
        {
            return gamePadStates[(int)index].IsButtonDown(button) &&
            lastGamePadStates[(int)index].IsButtonUp(button);
        }
        public static bool ButtonDown(Buttons button, PlayerIndex index)
        {
            return gamePadStates[(int)index].IsButtonDown(button);
        }
        #endregion
        #region Mouse Region
        public static bool LeftMouseDown()
        {
            return mouseState.LeftButton == ButtonState.Pressed && lastMouseState.LeftButton == ButtonState.Released;
        }
        public static bool LeftMouseUp()
        {
            return mouseState.LeftButton == ButtonState.Released && lastMouseState.LeftButton == ButtonState.Pressed;
        }
        public static bool LeftMouseClicked()
        {
            return mouseState.LeftButton == ButtonState.Pressed;
        }

        public static bool RightMouseDown()
        {
            return mouseState.RightButton == ButtonState.Pressed && lastMouseState.RightButton == ButtonState.Released;
        }
        public static bool RightMouseUp()
        {
            return mouseState.RightButton == ButtonState.Released && lastMouseState.RightButton == ButtonState.Pressed;
        }
        public static bool RightMouseClicked()
        {
            return lastMouseState.RightButton == ButtonState.Pressed;
        }

        public static bool MiddleMouseDown()
        {
            return mouseState.MiddleButton == ButtonState.Pressed && lastMouseState.MiddleButton == ButtonState.Released;
        }
        public static bool MiddleMouseClicked()
        {
            return mouseState.MiddleButton == ButtonState.Released && lastMouseState.MiddleButton == ButtonState.Pressed;
        }

        public static bool MouseWheelUp()
        {
            return mouseState.ScrollWheelValue > lastMouseState.ScrollWheelValue;
        }

        public static bool MouseWheelDown()
        {
            return mouseState.ScrollWheelValue < lastMouseState.ScrollWheelValue;
        }

        #endregion
    }
}
